package com.itheima.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.itheima.entity.Result;
import com.itheima.resources.MessageConstant;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;



import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import java.util.Map;
@RestController
@RequestMapping(value = "/report",name = "报表模块")
public class ReportController {
    /*  @RequestMapping(value = "/getMemberReport.do", name = "每个月的会员数量折线图")
      public Result getMemberReport(){*/
    @Reference
    private MemberService memberService;

    @Reference
    private ReportService reportService;
    //性别饼状图
    @RequestMapping("getMemberBySex.do")
    public Result getMemberBySex() {

        try {
            List<Map<String, String>> mapList = memberService.getMemberBySex();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    //年龄饼状图
    @RequestMapping("getMemberByAge.do")
    public Result getMemberByAge () {
        try {
            List<Map> mapList = memberService.getMemberByAge();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    //日期折行图
    @RequestMapping("getMemberByDate.do")
    public Result getMemberByDate(@RequestParam("dates") String dates){
        try {
            //接收前端传过来的数据(String类型) 切割获得相应的日期
            String[] split = dates.split(",");
            String[] split1 = split[0].split("-");
            String beginDate = split1[0] + "-" + split1[1];
            String[] split2 = split[1].split("-");
            String endDate = split2[0] + "-" + split2[1];
            Map map = memberService.getMemberByDate(beginDate, endDate);
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
    @RequestMapping(value = "/getSetMealReport.do", name = "预约占比")
    public Result getSetMealReport () {
        try {
            //调用生产者查询每个月的会员数量
            List<Map> list = reportService.getSetMealReport();
            return new Result(true, "查询预约占比成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "查询预约占比失败");
        }

    }

    @RequestMapping(value = "/getBusinessData.do", name = "查询运营数据")
    public Result getBusinessData () {
        try {
            //调用生产者
            Map map = reportService.getBusinessData();
            return new Result(true, "查询运营数据成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "查询运营数据失败");
        }

    }
    @RequestMapping(value = "/exportExcel.do", name = "导出Excel数据")
    public void exportExcel(@RequestBody Map<String,Object> map, HttpSession session, HttpServletResponse reportService){
        try {
            //使用session获取ServletContext域对象,再获取到Excel模板文件的路径
            String realPath = session.getServletContext().getRealPath("/template/report_template.xlsx");

            //装载excel模板
            TemplateExportParams params = new TemplateExportParams(realPath);

            //使用easyPoi提供的工具类,封装模板对象以及map对象
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);

            //游览器下载
            //定义文件名
            String filename = "report_template.xls";
            //转换mime类型
            String mimeType = session.getServletContext().getMimeType(filename);
            System.out.println(mimeType);
            //设置响应头
            reportService.setHeader("Content-Disposition", "attachment; filename=" + filename);
            //设置响应的mime类型
            reportService.setContentType(mimeType);
            //获取响应对象的输出流
            ServletOutputStream os = reportService.getOutputStream();
            System.out.println(workbook);
            //输出
            workbook.write(os);
            //释放资源
            //os.close();

            System.out.println("下载了");

            /*--本地直接下载
            //创建文件对象
            File file = new File("D:/excel/");
            //如果没有就以这个路径创建文件夹
            if (!file.exists()){
                file.mkdirs();
            }

            //创建输出流
            FileOutputStream fos = new FileOutputStream("D:/excel/report_template.xlsx");
            //输出
            workbook.write(fos);
            //释放资源
            fos.close();*/
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}

